/**
 *	@file	fld_pgear.c
 *	@brief	ポケギアフィールド呼び出し処理
 *	@date	07.07.13
 *	@author	Miyuki Iwasawa
 */

#include "common.h"

#include "fieldsys.h"

#include "field_subproc.h"
#include "field/field.h"
#include "field/situation_local.h"
#include "field/script.h"
#include "field/syswork.h"
#include "field/fld_bgm.h"
#include "fieldmap.h"
#include "zonedata.h"
#include "system/mystatus.h"
#include "application/pokegear.h"
#include "tel/pmtel_def.h"
#include "savedata/pgear_sv_util.h"
#include "savedata/misc.h"
#include "fielddata/maptable/zone_id.h"
#define __FLD_PGEAR_H_GLOBAL
#include "fld_pgear.h"

#include "fld_pmtel.h"

static void sub_CallDataSetCore(FIELDSYS_WORK* fsys,PGEAR_PARAM* pp,BOOL sky_f)
{
    MYSTATUS* pMyStatus;
	int i = 0,pos = 0,ret = 0;

	EVENTWORK * ev = SaveData_GetEventWork(fsys->savedata);
	SITUATION * sit = SaveData_GetSituation(fsys->savedata);
	LOCATION_WORK * escape = Situation_GetEscapeLocation(sit);
	LOCATION_WORK * now = Situation_GetNowLocation(sit);
	FLD_PMTEL_WORK* ptw = FldPMTel_PointerGet(fsys);

	//セーブデータポインタ引継ぎ
	pp->save = fsys->savedata;

	pp->player_zone = now->zone_id;	//自機が今実際にいるゾーン

//	pp->player_zone = PGMAP_SHINTO_ZONE;
//	pp->player_zone = PGMAP_SHIP_ZONE;
	{
		if(ZoneData_IsSinouField(pp->player_zone)){
			pp->player_x = Player_NowGPosXGet( fsys->player );
			pp->player_z = Player_NowGPosZGet( fsys->player );
			pp->tmap_gx = pp->player_x/BLOCK_GRID_W;
			pp->tmap_gz = pp->player_z/BLOCK_GRID_W;
			pp->field_zone = pp->player_zone;
		}else{
			WORLD_MAP_PTR world;
			s16 x,z;

			pp->player_x = escape->grid_x;
			pp->player_z = escape->grid_z;

			ZoneData_GetTMapPlayerPos(pp->player_zone,&x,&z);

			world = WorldMapInit();
			SetUpWorldMatrix(ZONE_ID_T20, world);	//適当なフィールド上のゾーンを渡して、フィールドマトリクスを指定
			if(x == 0 && z == 0){
				pp->tmap_gx = pp->player_x/BLOCK_GRID_W;
				pp->tmap_gz = pp->player_z/BLOCK_GRID_W;
			}else{
				pp->tmap_gx = x;
				pp->tmap_gz = z;
			}
			pp->field_zone = World_GetZoneIDFromMatrixXZ(world,pp->tmap_gx,pp->tmap_gz);
			WorldMapRelease(world);
		}
	}

	//自機の性別
    pMyStatus = SaveData_GetMyStatus(GameSystem_GetSaveData(fsys));
	pp->player_sex = MyStatus_GetMySex(pMyStatus);

	//操作モードの引継ぎ
	pp->pKTStatus = &fsys->KeyTouchStatus;
	pp->key_mode = GameSystem_CheckTouchOrKey(fsys);

	//フィールドBGMNOを取得
	pp->fld_bgm = Snd_FieldBgmNoGet(fsys,pp->player_zone);

	if(sky_f){
		return;
	}
	//呼び出しモード設定
	if( FldPMTel_CallupFlagGet( ptw )){	//着信アリ
		PMTEL_CALL_PARAM prm;

		pp->tel_number = FldPMTel_CallParamGet(ptw,&prm);
		if(pp->tel_number >= PMTEL_NUMBER_MAX){
			pp->mode = PGEAR_CALL_NORMAL;	//保険
			return;
		}
		//パラメータ格納
		pp->tel_id = prm.tel_id;
		pp->tel_type = prm.tel_type;

		//カウンター着信(予約)だった場合は、フラグリセット
		if(pp->tel_type == TEL_RECEIVE_COUNTER){
			PMTelSv_CallReserveReset(SaveData_GetPMTelSave(fsys->savedata),prm.reserve_id);
		}else if(pp->tel_type == TEL_CALL_NORMAL){
			//ノーマル着信(ランダム着信)だった場合は、着信フラグをセット
			MISC_PMTelNumberStockAdd(SaveData_GetMisc(fsys->savedata),prm.group,prm.group_num,pp->tel_number);
		}
		//着信フラグリセット
		FldPMTel_ResetCallParam(ptw);
		pp->mode = PGEAR_CALL_TEL;
	}else{
		pp->mode = PGEAR_CALL_NORMAL;
	}
}

/**
 *	@brief	フィールド　ポケギアモジュール呼び出しデータセット
 *
 *	@param	fsys	FIELDSYS_WORK*
 *	@param	pp		PGEAR_PARAM*:データを格納する構造体型へのポインタ
 */
void FieldPGear_CallDataSet(FIELDSYS_WORK* fsys,PGEAR_PARAM* pp)
{
	sub_CallDataSetCore(fsys,pp,FALSE);
}

/**
 *	@brief	フィールド　ポケギアモジュール呼び出しデータセット　空を飛ぶ専用
 *
 *	@param	fsys	FIELDSYS_WORK*
 *	@param	pp		PGEAR_PARAM*:データを格納する構造体型へのポインタ
 */
void FieldPGearSky_CallDataSet(FIELDSYS_WORK* fsys,PGEAR_PARAM* pp)
{
	sub_CallDataSetCore(fsys,pp,TRUE);
}
